var t = require("../../../../@babel/runtime/helpers/defineProperty"), s = [], e = 0, o = getApp();

Component({
    options: {
        multipleSlots: !0
    },
    properties: {
        show: {
            type: Boolean,
            value: !1
        },
        isRent: {
            type: Boolean,
            value: !1
        },
        themeColor: {
            type: String,
            value: "#f14e4e"
        },
        goods: {
            type: Object,
            value: function() {
                return {};
            }
        },
        goodSpecs: {
            type: Array,
            value: function() {
                return [];
            }
        },
        options: {
            type: Array,
            value: function() {
                return [];
            }
        },
        disableStepperInput: {
            type: Boolean,
            value: !1
        },
        quota: {
            type: Number,
            value: -1
        },
        startSaleNum: {
            type: Number,
            value: 1
        },
        hiddenBuyBtn: {
            type: Boolean,
            value: !1
        }
    },
    data: {
        popThumb: "",
        popPrice: "",
        popStock: "",
        popDescription: "",
        popUnit: "",
        optionsMaxCount: 0,
        selectedNum: 1,
        skuImages: [],
        specs: []
    },
    observers: {
        show: function(t, s) {
            t && this.initPopView();
        }
    },
    methods: {
        initPopView: function() {
            console.log("初始化顶部商品信息", this.data.goods), this.setData({
                popThumb: this.data.goods.hasOwnProperty("thumb") ? this.data.goods.thumb : "",
                popPrice: this.data.goods.hasOwnProperty("price") ? this.data.goods.price : "",
                popStock: this.data.goods.hasOwnProperty("stock") ? this.data.goods.stock : null,
                popUnit: this.data.goods.hasOwnProperty("unit") ? this.data.goods.unit : "",
                specs: this.data.goodSpecs,
                popDescription: null
            }), s = [], e = 0, this.initSkuIMG(), this.initSku();
        },
        initSku: function() {
            var t = this;
            if (1 == this.data.goods.has_option) {
                if (console.log("00000000000", this.data.specs, this.data.options), 0 == this.data.specs.length || 0 == this.data.options.length) return;
                console.log("sssssssssss"), this.data.options.forEach(function(s, e) {
                    var i = o._isTextEmpty(s.thumb) ? t.data.goods.thumb : s.thumb;
                    t.data.skuImages.push(i);
                });
                for (var s = 0; s < this.data.specs.length; s++) this.data.specs[s].description = this.data.specs[s].specitem[0], 
                console.log("1111111111111111111"), this.selectSpecs(this.data.specs[s].specitem[0], "first", s);
            } else this.data.goods.thumb && this.data.skuImages.push(this.data.goods.thumb);
            this.setData({
                skuImages: this.data.skuImages,
                specs: this.data.specs,
                optionsMaxCount: this.data.popStock
            });
        },
        getSkuData: function() {
            return {
                price: this.data.popPrice,
                stock: this.data.popStock,
                selectedNum: this.data.selectedNum,
                has_option: this.data.goods.has_option,
                optionsId: e
            };
        },
        buyClicked: function() {
            this.triggerEvent("buy-clicked", this.getSkuData());
        },
        stepperPlus: function() {
            this.triggerEvent("stepper-plus", this.getSkuData());
        },
        stepperMinus: function() {
            this.triggerEvent("stepper-minus", this.getSkuData());
        },
        stepperChange: function() {
            this.triggerEvent("stepper-change", this.getSkuData());
        },
        setPrice: function(t) {
            t && this.setData({
                popPrice: t
            });
        },
        setStock: function(t) {
            t && this.setData({
                popStock: t
            });
        },
        selectSpecs: function(s, e, o) {
            var i = {}, a = 0;
            if ("first" == e ? (a = o, i = s) : "sku_swiper" == e ? i = s : (a = s.currentTarget.dataset.specsidex, 
            i = s.currentTarget.dataset.specitem), i.c) return !1;
            this.setData(t({}, "specs[" + a + "].description", i.id)), this.manageSpecs(i), 
            this.setGoodsSpecs(i), this.setGoodsSpecsChangeInfo(), this.getMaxCount();
        },
        manageSpecs: function(t) {
            var e = {};
            if (e.id = t.id, e.specid = t.specid, e.title = t.title, s.length > 0) {
                for (var o = 0; o < s.length; o++) s[o].specid == e.specid && s.splice(o, 1);
                s.push(e);
            } else s.push(e);
            if (s.length == this.data.specs.length) {
                for (var i = [], a = 0; a < this.data.specs.length; a++) for (var n = 0; n < s.length; n++) if (this.data.specs[a].id == s[n].specid) {
                    i.push(s[n]);
                    break;
                }
                s = i;
            }
            this.setGoodsDescription();
        },
        setGoodsDescription: function() {
            var t = "";
            if (s.length == this.data.specs.length) {
                t = "已选择 ";
                for (var e = 0; e < s.length; e++) t += s[e].title + " ";
            } else {
                t = "请选择 ";
                for (var o = 0; o < this.data.specs.length; o++) for (var i = 0; i < s.length; i++) if (this.data.specs[o].id != s[i].specid) {
                    t += this.data.specs[o].title + " ";
                    break;
                }
            }
            this.setData({
                popDescription: t
            });
        },
        setGoodsSpecs: function(t) {
            for (var s = 0; s < this.data.specs.length; s++) t.specid != this.data.specs[s].id && this.setGoodsSpecsStatus(this.data.specs[s].specitem, t.id);
        },
        setGoodsSpecsStatus: function(t, s) {
            for (var e = [], o = 0; o < this.data.options.length; o++) for (var i = this.data.options[o].specs.split("_"), a = 0; a < i.length; a++) if (i[a] == s) return void e.push(this.data.options[o]);
            for (var n = 0; n < e.length; n++) for (var p = e[n].specs.split("_"), h = 0; h < p.length; h++) if (p[h] != s && 0 == e[n].stock) {
                for (var r = 0; r < t.length; r++) if (p[h] == t[r].id) return void (t[r].c = !0);
            } else if (p[h] != s && e[n].stock > 0) for (var d = 0; d < t.length; d++) if (p[h] == t[d].id) return void (t[d].c = !1);
            console.log(e);
        },
        setGoodsSpecsChangeInfo: function() {
            if (s.sort(function(t, s) {
                return t.id - s.id;
            }), s.length == this.data.specs.length) {
                for (var t = "", i = 0; i < s.length; i++) t += s[i].id + "_";
                t = t.substring(0, t.length - 1);
                for (var a = 0; a < this.data.options.length; a++) if (t == this.setGoodsSpecsBySort(this.data.options[a].specs)) {
                    this.setData({
                        activeSkuIndex: a,
                        popPrice: this.data.options[a].product_price,
                        popThumb: o._isTextEmpty(this.data.options[a].thumb) ? this.data.goods.thumb : this.data.options[a].thumb,
                        popStock: this.data.options[a].stock,
                        optionsMaxCount: this.data.options[a].stock
                    }), e = this.data.options[a].id, this.data.optionsMaxCount > 0 && this.setData({
                        popNum: 1,
                        goodsCount: 1
                    }), console.log("=================", e), this.triggerEvent("sku-selected", e);
                    break;
                }
            }
        },
        setGoodsSpecsBySort: function(t) {
            var s = t.split("_");
            s.sort(function(t, s) {
                return t - s;
            });
            for (var e = "", o = 0; o < s.length; o++) e += s[o] + "_";
            return e = e.substring(0, e.length - 1);
        },
        getMaxCount: function() {
            s.length == this.data.specs.length && (0 == this.data.optionsMaxCount && (this.data.goodsCount = 0), 
            this.data.goodsCount > this.data.optionsMaxCount && (this.data.goodsCount = this.data.optionsMaxCount), 
            this.setData({
                goodsCount: this.data.goodsCount
            }));
        },
        close: function() {
            this.setData({
                show: !1
            });
        },
        initSkuIMG: function() {}
    }
});